package jwj8508.mvc.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jwj8508.mvc.dao.BoardDAO;
import jwj8508.mvc.dao.MemberDAO;
import jwj8508.mvc.model.BoardVO;
import jwj8508.mvc.model.MemberVO;

public class LoginAction implements ActionHandler {
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		String viewPage = "f|/WEB-INF/jsp/tiles/mvc.jsp";
		String method = req.getMethod();
		if (method.equalsIgnoreCase("get")) {
			req.setAttribute("action", "../member/login.jsp");
		} else if (method.equalsIgnoreCase("post")) {
			MemberVO m = new MemberVO();
			m.setUid(req.getParameter("uid"));
			m.setPwd(req.getParameter("pwd"));
			if (MemberDAO.login(m)) {
				HttpSession sess = req.getSession();
				sess.setAttribute("uid", m.getUid());
				sess.setAttribute("pwd", m.getPwd());
				viewPage = "r|index.mvc";
			} else {
				req.setAttribute("action", "../member/loginfail.jsp");
			}
		}
		return viewPage;
	}
}
